<template>
    <el-dialog
        title="上传论文"
        :visible.sync="dialogVisible"
        width="30%"
        :close-on-click-modal='false'
        >
        <el-upload
            class="upload-demo"
            :action="http+'/admin/MemberApply/upload'"
            :file-list="fileList"
            :data='{

            }'
            :multiple='false'
            accept='image/*,application/msword,text/plain,application/pdf'
            name='img'
            :on-success='success'
            :on-remove="handleRemove"
            list-type="picture">
            <div slot="tip" class="el-upload__tip">请上传pdf、word或图片类型文件</div>
            <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
        <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="commit">保 存</el-button>
        </span>
    </el-dialog>
</template>

<script>
import {updateApply} from '@/api/admin.js'
import {
  getapplyid,
} from "@/utils/auth";
export default {
    data() {
        return {
            dialogVisible:false,
            fileList:[],
            http:process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API : window.location.href.slice(0, window.location.href.indexOf('/user')),

        }
    },
    methods: {
        handleClose(){
            this.dialogVisible=false
            this.fileList=[]
        },
        open(list){
            this.dialogVisible=true
            this.fileList=list
        },
        success(res,file){
            if(res.code!=200){
                this.$message.error(res.msg)
                this.fileList=[]
            }else{
                if(file.raw.type.includes('image/')){
                    this.fileList.push({...res.data,name:res.data.filename,fileType:file.raw.type})
                }else{
                    this.fileList = [{...res.data,name:res.data.filename,fileType:file.raw.type}]
                }
                this.$message.success(res.msg)
            }
        },
        handleRemove(file){
            let list = JSON.parse(JSON.stringify(this.fileList))
            this.fileList=list.filter(item=>{
                return item.uid!=file.uid
            })
            console.log(file,this.fileList)
        },
        commit(){
            updateApply({applyid:getapplyid(),paper:JSON.stringify(this.fileList)}).then(res=>{
                if(res.code==200){
                    this.$message.success(res.msg)
                    this.handleClose()
                    this.$emit('seachLeng')
                }else{
                    this.$message.error(res.msg)
                }
            })
        }
    },
}
</script>

<style>

</style>